package sopia.conn;

import sopia.cmm.CommonFunct;
import sopia.cmm.Constant;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ExpenseAdapter {
	 /** Called when the activity is first created. */
	private DBAdapter dbAdapter ;
	private SQLiteDatabase db;  

	public ExpenseAdapter(Context context){
		  dbAdapter = new DBAdapter(context);
		  db = dbAdapter.getSqlDB();
	}
	
	public void beginTransaction(){
		db.beginTransaction();
	}
	public void setTransactionSuccessful(){
		db.setTransactionSuccessful();
	}
	public void endTransaction(){
		db.endTransaction();
	}
	
    public void close() 
    {
    	dbAdapter.close();
    }
    
    public long insertExpense(String typeNameExp, String nameAccountExp, String nameCategoryExp, String amountExp) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(SQLQuery.KEY_TYPENAME_EXP, typeNameExp);
        initialValues.put(SQLQuery.KEY_NAME_ACCOUNT, nameAccountExp);
        initialValues.put(SQLQuery.KEY_NAME_CATEGORY_EXP, nameCategoryExp);
        initialValues.put(SQLQuery.KEY_AMOUNT_EXP, amountExp);
        return db.insert(SQLQuery.DB_EXPENSE_TABLE, null, initialValues);
    }

    public boolean deleteExpenseByAccount(String nameAccount) 
    {
        return db.delete(SQLQuery.DB_EXPENSE_TABLE, CommonFunct.join(SQLQuery.KEY_NAME_ACCOUNT,
				" = '",nameAccount,"'"), null) > 0;
    }
    public boolean deleteExpenseByCategory(String category) 
    {
        return db.delete(SQLQuery.DB_EXPENSE_TABLE, CommonFunct.join(SQLQuery.KEY_NAME_CATEGORY_EXP,
				" = '", category,"'"), null) > 0;
    }
    public boolean deleteExpenseByAccountAndExpense(String nameExpense, String curNameAccount) 
    {
        return db.delete(SQLQuery.DB_EXPENSE_TABLE, CommonFunct.join(SQLQuery.KEY_TYPENAME_EXP,
				" = '",nameExpense,"' AND ", SQLQuery.KEY_NAME_ACCOUNT , " = '", curNameAccount, "'"), null) > 0;
    }

    //---retrieves all the accounts---
    public Cursor getAllExpenseByAccountAndCategory(String accountName , String category) 
    {
        return db.query(SQLQuery.DB_EXPENSE_TABLE, new String[] {
        		SQLQuery.KEY_TYPENAME_EXP}, 
                SQLQuery.KEY_NAME_ACCOUNT + " = '" + accountName +"' AND " 
                + SQLQuery.KEY_NAME_CATEGORY_EXP + " = '" + category +"'", 
                null, 
                null, 
                null, 
                null);
    }
    public Cursor getAllExpenseByAccount(String nameAccount) 
    {
        return db.query(SQLQuery.DB_EXPENSE_TABLE, new String[] {
        		SQLQuery.KEY_TYPENAME_EXP}, 
                SQLQuery.KEY_NAME_ACCOUNT + " = '" + nameAccount +"'", 
                null, 
                null, 
                null, 
                null);
    }
    public Cursor getAmountByAccount(String nameAccount) throws SQLException 
    {
    	return db.query(SQLQuery.DB_EXPENSE_TABLE, new String[] {
        		SQLQuery.KEY_AMOUNT_EXP}, 
                SQLQuery.KEY_NAME_ACCOUNT + " = '" + nameAccount +"'", 
                null, 
                null, 
                null, 
                null);
    }
    public Cursor getAmountByTypeExpenseAccount(String typeExpense, String nameAccount) throws SQLException 
    {
    	return db.query(SQLQuery.DB_EXPENSE_TABLE, new String[] {
        		SQLQuery.KEY_AMOUNT_EXP}, 
                CommonFunct.join(SQLQuery.KEY_NAME_ACCOUNT,
                		" = '",
                		nameAccount,
                		"' AND ",
                		SQLQuery.KEY_TYPENAME_EXP,
                		" = '",
                		typeExpense,
                		"'"),
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves a particular account---
    public Cursor getAllTypesExpense() throws SQLException 
    {
          return db.query(SQLQuery.DB_EXPENSE_TABLE, new String[] {
        		SQLQuery.KEY_TYPENAME_EXP,
        		SQLQuery.KEY_AMOUNT_EXP}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }
/*
    //---updates a account---
    public boolean updateAccount( String nameAccount, 
    String amount) 
    {
        ContentValues args = new ContentValues();
        args.put(SQLQuery.KEY_AMOUNT_ACCOUNT, amount);
        return db.update(SQLQuery.DB_ACCOUNT_TABLE, args, 
        		SQLQuery.KEY_NAME_ACCOUNT + " = " + nameAccount, null) > 0;
    }
    */
}
